﻿using SmartSpace.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SmartSpace.Template.Enterprise
{
    public partial class HeaderPanel : System.Web.UI.UserControl, IHeaderPanel, INavigationPanel
    {
        private IEnumerable<Core.Navigation> _navigations;
        private Core.User _user;

        protected void Page_Load(object sender, EventArgs e)
        {
            _ctlImage.ImageUrl = string.Format("~/_handlers/_private/get-image.ashx?id={0}", SmartSpace.Web.SsSite.Current.ImageId);
        }

        public event LogoffEventHandler Logoff;

        public void Initiate(IEnumerable<SmartSpace.Core.Shortcut> shortcuts, List<SmartSpace.Core.Site> applications, SmartSpace.Core.Site currentApplication, SmartSpace.Core.User loginUser)
        {
            _user = loginUser;
            this._ctlApplicationName.Text = currentApplication.Title;
            this._ctlApplicationShortName.Text = currentApplication.ShortTitle;
            this._ctlBtnUser.Text = _user.Name;
        }

        protected void _ctlBtnLogoff_Click(object sender, EventArgs e)
        {
            if (Logoff != null)
            {
                Logoff(this, new LogoffEventArgs(_user));
            }
        }

        protected void _ctlBtnUser_Click(object sender, EventArgs e)
        {

        }

        public void Initiate(IEnumerable<Core.Navigation> navigations, IEnumerable<Core.Shortcut> shortcuts)
        {
            _navigations = navigations;

            if (!IsPostBack)
            {
                _ctlListMenu.DataSource = navigations;
                _ctlListMenu.DataBind();
            }
        }

        private void BuildMenu(MenuItem parentItem, Core.Navigation parentNavigation)
        {
            foreach (var item in parentNavigation.Children)
            {
                MenuItem menuItem = new MenuItem(item.Caption, item.Id.ToString());
                parentItem.ChildItems.Add(menuItem);
            }
        }

        public event NavigatingEventHandler Navigating;

        protected void _ctlMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            Guid navigationId = new Guid(e.Item.Value);
            Core.Navigation navigation = GetNavigation(navigationId);

            if (Navigating != null)
            {
                Navigating(this, new NavigatingEventArgs(e.Item.Text, navigation));
            }
        }

        private Core.Navigation GetNavigation(Guid navigationId)
        {
            foreach (var item in _navigations)
            {
                if (item.Id == navigationId)
                {
                    return item;
                }

                Core.Navigation match = GetNavigation(item, navigationId);
                if (match != null)
                {
                    return match;
                }
            }
            return null;
        }

        private Core.Navigation GetNavigation(Core.Navigation navigation, Guid navigationId)
        {
            if (navigation.Children == null)
            {
                return null;
            }

            foreach (var item in navigation.Children)
            {
                if (item.Id == navigationId)
                {
                    return item;
                }

                Core.Navigation match = GetNavigation(item, navigationId);
                if (match != null)
                {
                    return match;
                }
            }
            return null;
        }

        public string ParseUrl(object url)
        {
            string urlString = url.ToString();

            if (urlString.StartsWith("p:>>"))
            {
                return urlString.Replace("p:>>", string.Empty);
            }
            return urlString;
        }

        public string GetStateCssClass(object url)
        {
            string urlString = url.ToString();
            string pageName = Request.QueryString["_page"];
            if (string.Format("{0}.ssvp",pageName) == urlString)
            {
                return "active";
            }
            return pageName;
        }
    }
}